#include "ResourceHashTable.h"
//necessary
#include "memory-tools.h"

//TODO testing
void initResourceHashTable(PresourceHashTable phashtable){
	*phashtable=NULL;
}

//TODO testing
PresourceElement findResourceInHashTable(PresourceHashTable phashtable,const char* key){
	PresourceHTCell result;
	HASH_FIND_STR(*phashtable,key,result);
	if (result==NULL){
		return NULL;
	}
	return result->resource;
}

//TODO testing
void addResourceInHashTable(PresourceHashTable phashtable, PresourceElement resource){
	PresourceHTCell pcell=initResourceHTCell(resource);
	HASH_ADD_KEYPTR(hh,*phashtable,pcell->key,strlen(pcell->key),pcell);
}

//TODO testing
void freeResourceHashTable(PresourceHashTable phashtable){
	PresourceHTCell element;
	PresourceHTCell tmp;

	HASH_ITER(hh,*phashtable,element,tmp){
		HASH_DEL(*phashtable,element);
		freeResourceHTCell(element);
	}
}

//TODO testing
void generateDOTResourceHashTable(FILE* f,PresourceHashTable phashtable,int* start){
	int items;
	int i;

	fprintf(f,"subgraph {\n");
	fprintf(f,"\trankdir=LR;\n");
	items=HASH_COUNT(*phashtable);
	fprintf(f,"\ttable [shape=\"record\" fillcolor=\"green\" label=\"");

	for (i=0;i<items;i++){
		fprintf(f,"<f%04d>|",*start+i);
	}

	fprintf(f,"\"];\n");

	//generation of the nodes atached to the hashtable
	ResourceHashTable tmp;
	for (tmp=*phashtable; tmp!=NULL; tmp=tmp->hh.next, *start=*start+1){
		fprintf(f,"\t");
		//generateDOTResourceHTCell(f,tmp,*start);
		fprintf(f,"\n");
		fprintf(f,"\ttable:f%04d -> n%04d;\n",*start,*start);
	}
	fprintf(f,"}\n");
}
